package com.ruoyi.pg.questionnaire.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pg.questionnaire.vo.PgQuestionVo;
import com.ruoyi.pg.questionnaire.service.IPgQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 问卷题目Controller
 *
 * @author Ming
 * @date 2025-08-22
 */
@Api(tags = "问卷题目Controller")
@RestController
@RequestMapping("/questionnaire/question")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PgQuestionController extends BaseController {

    private final IPgQuestionService pgQuestionService;

    @ApiOperation("查询问卷题目列表")
    @GetMapping("/list")
    public TableDataInfo<PgQuestionVo> list(PgQuestionVo entity) {
        return pgQuestionService.queryList(entity);
    }

    @ApiOperation("查询问卷题目所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(PgQuestionVo entity) {
        return AjaxResult.success("查询成功", pgQuestionService.queryAll(entity));
    }

    @ApiOperation("导出问卷题目列表")
    @Log(title = "问卷题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PgQuestionVo entity) {
        List<PgQuestionVo> list = pgQuestionService.queryAll(entity);
        ExcelUtil<PgQuestionVo> util = new ExcelUtil<>(PgQuestionVo.class);
        util.exportExcel(response, list, "问卷题目数据");
    }

    @ApiOperation("获取问卷题目详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", pgQuestionService.queryById(id));
    }

    @ApiOperation("新增问卷题目")
    @Log(title = "问卷题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PgQuestionVo entity) {
        return toAjax(pgQuestionService.save(entity));
    }

    @ApiOperation("修改问卷题目")
    @Log(title = "问卷题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PgQuestionVo entity) {
        return toAjax(pgQuestionService.updateById(entity));
    }

    @ApiOperation("删除问卷题目")
    @Log(title = "问卷题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pgQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
